﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Alumnado.Models;

namespace Alumnado.Controllers
{
    public class AlumnoController : Controller
    {
        private ContextoDatos db = new ContextoDatos();
        private List<String> mats = new List<String>();

        //
        // GET: /Alumno/

        public ActionResult Index()
        {
            matsViewBag();
            return View(db.alumnos.ToList());
        }

        //
        // GET: /Alumno/Details/5

        public ActionResult Details(int id = 0)
        {
            int pro = 0;
            int cant = 0;
            List<Materia> mates = new List<Materia>();
            Alumno alumno = db.alumnos.Find(id);
            if (alumno == null)
            {
                return HttpNotFound();
            }
            foreach (var i in db.Materias)
            {
                if (i.alumnoid == id)
                {
                    pro = pro + i.nota;
                    cant++;
                    mates.Add(i);
                }
            }
            ViewBag.lista = mates;
            if (cant != 0)
            { ViewBag.promedio = pro / cant; }
            return View(alumno);
        }

        //
        // GET: /Alumno/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Alumno/Create

        [HttpPost]
        public ActionResult Create(Alumno alumno)
        {
            if (ModelState.IsValid)
            {
                db.alumnos.Add(alumno);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(alumno);
        }

        //
        // GET: /Alumno/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Alumno alumno = db.alumnos.Find(id);
            if (alumno == null)
            {
                return HttpNotFound();
            }
            return View(alumno);
        }

        //
        // POST: /Alumno/Edit/5

        [HttpPost]
        public ActionResult Edit(Alumno alumno)
        {
            if (ModelState.IsValid)
            {
                db.Entry(alumno).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(alumno);
        }

        //
        // GET: /Alumno/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Alumno alumno = db.alumnos.Find(id);
            if (alumno == null)
            {
                return HttpNotFound();
            }
            return View(alumno);
        }

        //
        // POST: /Alumno/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Alumno alumno = db.alumnos.Find(id);
            db.alumnos.Remove(alumno);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult FiltradoCurso(int curso)
        {
            List<Alumno> alFil = new List<Alumno>();
            foreach (var i in db.alumnos)
            {
                if (i.curso == curso)
                {
                    alFil.Add(i);
                }
            }
            matsViewBag();
            return View("Index", alFil);
        }

        public void matsViewBag()
        {
            foreach (var i in db.Materias)
            {
                if (!mats.Contains(i.nombre))
                {
                    mats.Add(i.nombre);
                }
            }
            ViewBag.lista = mats;
        }
    }
}